package com.wj.algorithm;

public class P180 {
    public String binaryRepresentation(String n) {
        String e = "ERROR";
        if(n == null || n.length() < 1) {
            return e;
        }
        String[] arr = n.split("\\.");
        if(n.length() == 1) {
            return Long.toBinaryString(Long.parseLong(arr[0]));
        }
        
        double right = Double.parseDouble("."+arr[1]);
        
        String prev = Long.toBinaryString(Long.parseLong(arr[0]));
        String next = prev;
        if(right <= 0) {
            return next;
        }
        next += ".";
        int count = 32;
        
        while(count >= 0 && right > 0) {
            right = right * 2;
            if(right >= 1) {
                next += "1";
                right -= 1;
            } else {
                next += "0";
            }
            count --;
        }
        if(right > 0) {
            return e;
        }

        return next;
    }
    
    public static void main(String[] args) {
        System.out.println(new P180().binaryRepresentation("0.128121212121"));
    }
}
